package com.ruoyi.see.service.impl;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.see.mapper.ArticleSeeMapper;
import com.ruoyi.see.domain.ArticleSee;
import com.ruoyi.see.service.IArticleSeeService;

/**
 * 文章访问Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-10
 */
@Service
public class ArticleSeeServiceImpl implements IArticleSeeService {
    @Autowired
    private ArticleSeeMapper articleSeeMapper;

    /**
     * 查询文章访问
     *
     * @param uid 文章访问主键
     * @return 文章访问
     */
    @Override
    public ArticleSee selectArticleSeeByUid(Long uid) {
        return articleSeeMapper.selectArticleSeeByUid(uid);
    }

    /**
     * 查询文章访问列表
     *
     * @param articleSee 文章访问
     * @return 文章访问
     */
    @Override
    public List<ArticleSee> selectArticleSeeList(ArticleSee articleSee) {
        return articleSeeMapper.selectArticleSeeList(articleSee);
    }

    /**
     * 新增文章访问
     *
     * @param articleSee 文章访问
     * @return 结果
     */
    @Override
    public int insertArticleSee(ArticleSee articleSee) {
        articleSee.setCreateTime(DateUtils.getNowDate());
        return articleSeeMapper.insertArticleSee(articleSee);
    }

    /**
     * 修改文章访问
     *
     * @param articleSee 文章访问
     * @return 结果
     */
    @Override
    public int updateArticleSee(ArticleSee articleSee) {
        articleSee.setUpdateTime(DateUtils.getNowDate());
        return articleSeeMapper.updateArticleSee(articleSee);
    }

    /**
     * 批量删除文章访问
     *
     * @param uids 需要删除的文章访问主键
     * @return 结果
     */
    @Override
    public int deleteArticleSeeByUids(Long[] uids) {
        return articleSeeMapper.deleteArticleSeeByUids(uids);
    }

    /**
     * 删除文章访问信息
     *
     * @param uid 文章访问主键
     * @return 结果
     */
    @Override
    public int deleteArticleSeeByUid(Long uid) {
        return articleSeeMapper.deleteArticleSeeByUid(uid);
    }

    @Override
    public int countArticleSeeByUid(ArticleSee articleSee) {
        return articleSeeMapper.countArticleSeeByUid(articleSee);
    }

    @Override
    public int countArticleSeesByArticleIdAndDate(ArticleSee articleSee) {
        return articleSeeMapper.countArticleSeesByArticleIdAndDate(articleSee);
    }

    @Override
    public List<Integer> selectArticleSeeCountsByWeekForArticleId(ArticleSee articleSee1) {
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取当前周星期一的日期
        LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        // 打印从星期一到星期天的日期
        String[] dayNames = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            System.out.println(dayNames[i] + " 的日期是: " + monday.plusDays(i));
            LocalDate day = monday.plusDays(i);
            // 获取当前日期的起始时间点（00:00:00）
            LocalTime startOfDay = LocalTime.MIDNIGHT;
            LocalDateTime startOfToday = day.atTime(startOfDay);
            // 创建一个DateTimeFormatter对象，指定格式为yyyy-MM-dd HH:mm:ss
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // 使用formatter将LocalDateTime格式化为字符串
            String formattedDateTimeStart = startOfToday.format(formatter);

            // 如果你需要表示一天的真正结束（即下一天的开始减去一秒），可以这样做：
            LocalDateTime startOfTomorrow = day.plusDays(1).atTime(startOfDay);
            LocalDateTime endOfToday = startOfTomorrow.minusSeconds(1);
            // 创建一个DateTimeFormatter对象，指定格式为yyyy-MM-dd HH:mm:ss
            String formattedDateTimeStartEnd = endOfToday.format(formatter);

            System.out.println("今天的起始时间点是: " + formattedDateTimeStart);
            System.out.println("今天的结束时间点是: " + formattedDateTimeStartEnd);

            ArticleSee articleSee = new ArticleSee();
            articleSee.setArticleId(articleSee.getArticleId());

            Map map = new HashMap();
            map.put("beginCreateTime", formattedDateTimeStart);
            map.put("endCreateTime", formattedDateTimeStartEnd);
            articleSee.setParams(map);
            int num = articleSeeMapper.countArticleSeesByArticleIdAndDate(articleSee);
            list.add(num);
        }
        return list;
    }

    @Override
    public List<Integer> selectArticleSeeCountsByHourForArticleId(ArticleSee articleSee1) {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 创建一个DateTimeFormatter对象，用于格式化LocalDateTime为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<Integer> list = new ArrayList<>();

        // 迭代当天的每个小时
        for (int hour = 0; hour < 24; hour++) {
            // 创建每个小时的LocalDateTime对象
            LocalDateTime dateTime = today.atTime(LocalTime.of(hour, 0));
            LocalDateTime end = dateTime.plusHours(1).minusSeconds(1);
            // 格式化LocalDateTime为字符串
            String formattedDateTimeStart = dateTime.format(formatter);
            String formattedDateTimeEnd = end.format(formatter);

            ArticleSee articleSee = new ArticleSee();
            articleSee.setArticleId(articleSee1.getArticleId());

            Map map = new HashMap();
            map.put("beginCreateTime", formattedDateTimeStart);
            map.put("endCreateTime", formattedDateTimeEnd);
            articleSee.setParams(map);
            int num = articleSeeMapper.countArticleSeesByArticleIdAndDate(articleSee);
            list.add(num);
        }
        return list;
    }

    @Override
    public Integer selectArticleSeeCountsByMonthForArticleId(ArticleSee articleSee1) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 获取当前年份和月份
        YearMonth yearMonth = YearMonth.now();

        // 获取本月的第一天
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        // 设置起始日期的时间点为当天的00:00:00
        LocalDateTime startDateTime = firstDayOfMonth.atStartOfDay();

        LocalDate lastDayOfMonthCorrect = yearMonth.atEndOfMonth();
        // 设置结束日期的时间点为当天的23:59:59
        LocalDateTime endDateTime = lastDayOfMonthCorrect.atTime(LocalTime.MAX); // LocalTime.MAX 代表 23:59:59.99999999

        // 格式化LocalDateTime为字符串
        String formattedDateTimeStart = startDateTime.format(formatter);
        String formattedDateTimeEnd = endDateTime.format(formatter);

        ArticleSee articleSee = new ArticleSee();
        articleSee.setArticleId(articleSee1.getArticleId());

        Map map = new HashMap();
        map.put("beginCreateTime", formattedDateTimeStart);
        map.put("endCreateTime", formattedDateTimeEnd);
        articleSee.setParams(map);
        System.out.println(formattedDateTimeStart);
        System.out.println(formattedDateTimeEnd);
        int num = articleSeeMapper.countArticleSeesByArticleIdAndDate(articleSee);
        return num;
    }
}
